/**
 * @Title: YuYueServiceImpl.java
 * @Description: 用一句话描述该文件做什么
 * @author zhangxin
 * @date 2019年1月22日
 */
package com.fortune.rainshinesystem.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.ExceptionEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.exceptions.MethodException;
import com.fortune.rainshinecommon.model.system.YuYueModel;
import com.fortune.rainshinecommon.util.StringUtil;
import com.fortune.rainshinesystem.mapper.YuYueMapper;
import com.fortune.rainshinesystem.service.YuYueService;

/**
 * @ClassName: YuYueServiceImpl
 * @Description: 这里用一句话描述这个类的作用
 * @author zhangxin
 * @date 2019年1月22日
 */
@Service
public class YuYueServiceImpl implements YuYueService {

	@Autowired
	private YuYueMapper mapper;

	@Override
	public MsgModel list(ParmsModel parm) {
		try {
			Wrapper<YuYueModel> wrapper = new EntityWrapper<>();
			if (StringUtil.isNotBlank(parm.getSearchKey())) {
				wrapper.like(parm.getSearchKey(), parm.getSearchValue());
			}

			Page<YuYueModel> yuyuePage = new Page<>(parm.getPage(), parm.getLimit());
			List<YuYueModel> yuyueList = mapper.selectPage(yuyuePage, wrapper.orderBy("create_time", true));
			return new MsgModel(yuyuePage.getTotal(), yuyueList);
		} catch (Exception e) {
			e.printStackTrace();
			return new MsgModel(ResultCode.FAILURE);
		}
	}

	@Override
	@Transactional
	public boolean updateState(YuYueModel yuyue) {
		int i = 0;
		try {
			if (yuyue.getDealWith() != 0 && yuyue.getDealWith() != 1) {
				throw new MethodException(ExceptionEnum.VALIDATIONERROR);
			}
			 i = mapper.updateById(yuyue);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i > 0;
	}

	@Override
	@Transactional
	public boolean delete(YuYueModel yuyue) {
		int i = 0;
		try {
			 i = mapper.deleteById(yuyue.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return i > 0;
	}

	@Override
	public MsgModel addYuYue(YuYueModel info) {
		int res = mapper.insert(info);
		return new MsgModel(ResultCode.SUCCESS);
	}

}
